package cc.wsyw126.java.jvm.threadPoll;

import java.util.ArrayList;
import java.util.concurrent.*;

/**
 * Created by Administrator on 2017/4/14.
 */
public class testThread {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newCachedThreadPool();
        ArrayList<Future<String>> futures = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            futures.add(executorService.submit(new TaskWithResult(i)));
        }
        for (Future<String> future : futures) {
            try {
                String s = future.get();
                System.out.println("s = " + s);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }finally {
                executorService.shutdown();
            }
        }
    }

}

class TaskWithResult implements Callable<String> {
    private int id;

    public TaskWithResult(int id) {
        this.id = id;
    }

    @Override
    public String call() throws Exception {
        for (int i = 0; i < 1000; i++) {
            System.out.print("this is " + id + " " + i  + "\t");
        }
        System.out.println();
        return id + " Success";
    }
}